package com.danguo.happymonkey;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;

/**
 * This class implements our custom renderer. Note that the GL10 parameter
 * passed in is unused for OpenGL ES 2.0 renderers -- the static class GLES20 is
 * used instead.
 */
public class LessonOneRenderer implements GLSurfaceView.Renderer
{

   private static final double theFling = 0.95; //speed of scroll velocity decrease
   private GalacticPlane theGalacticPlane;
   private Points thePoints;
   private Lines theLines;
   private GLSurfaceView theContext;
   private float theCameraRotationX;
   private float theCameraRotationY;
   
   /**
    * Store the view matrix. This can be thought of as our camera. This matrix
    * transforms world space to eye space; it positions things relative to our
    * eye.
    */
   private float[] mViewMatrix = new float[16];

   /**
    * Store the projection matrix. This is used to project the scene onto a 2D
    * viewport.
    */
   private float[] mProjectionMatrix = new float[16];

   /**
    * Initialize the model data.
    */
   public LessonOneRenderer( GLSurfaceView aContext )
   {
      theContext = aContext;
      theCameraRotationX = -5;
      theCameraRotationY = 0;
   }

   @Override
   public void onSurfaceCreated( GL10 glUnused, EGLConfig config )
   {
      // Set the background clear color to gray.
      GLES20.glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );

      // Position the eye behind the origin.
      final float eyeX = 0.0f;
      final float eyeY = 2f;
      final float eyeZ = 1f;

      // We are looking toward the distance
      final float lookX = 0.0f;
      final float lookY = 0.0f;
      final float lookZ = -1.0f;

      // Set our up vector. This is where our head would be pointing were we
      // holding the camera.
      final float upX = 0.0f;
      final float upY = 1.0f;
      final float upZ = 0.0f;

      // Set the view matrix. This matrix can be said to represent the camera
      // position.
      // NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination
      // of a model and
      // view matrix. In OpenGL 2, we can keep track of these matrices
      // separately if we choose.
      Matrix.setLookAtM( mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ,
            upX, upY, upZ );
      
//      Matrix.translateM( mViewMatrix, 0, 0, 0, 10 );
      //Matrix.rotateM( mViewMatrix, 0, 45f, 1f, 0f, 0f );

      theGalacticPlane = new GalacticPlane( theContext );
      thePoints = new Points( theContext );
      theLines = new Lines( new float[0][0] );
      Line newLine = new Line();
      newLine.setVerts( -3f, 0f, 1f, 1f, 1f, 5f );
      newLine.setColor( 0f, 1f, 0f, 1f );
      theLines.add( newLine );
      newLine = new Line();
      newLine.setVerts( -3f, 0f, 1f, 2f, -1f, -4f );
      newLine.setColor( 0f, 1f, 0f, 1f );
      theLines.add( newLine );
   }

   @Override
   public void onSurfaceChanged( GL10 glUnused, int width, int height )
   {
      // Set the OpenGL viewport to the same size as the surface.
      GLES20.glViewport( 0, 0, width, height );

      // Create a new perspective projection matrix. The height will stay the
      // same
      // while the width will vary as per aspect ratio.
      float ratio = (float) width/height;
      float left = -ratio;
      float right = ratio;
      float top = 1;
      float bottom = -1;
      float near = 1f;
      float far = 15.0f;

      Matrix.frustumM( mProjectionMatrix, 0, left, right, bottom, top, near,
            far );
   }
   
   public void setCameraRotation( float theF, float theF2 )
   {
      theCameraRotationX = theF;
      theCameraRotationY = theF2;
   }

   @Override
   public void onDrawFrame( GL10 glUnused )
   {
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT );

      theCameraRotationX *= theFling;
      theCameraRotationY *= theFling;
      Matrix.rotateM( mViewMatrix, 0, theCameraRotationX, 0f, 1f, 0f );

      theGalacticPlane.draw( mViewMatrix, mProjectionMatrix );
      theLines.draw( mViewMatrix, mProjectionMatrix );
      //GLES20.glEnable( GLES20.GL_DEPTH_TEST );
      
      thePoints.draw( mViewMatrix, mProjectionMatrix );
      //GLES20.glDisable( GLES20.GL_DEPTH_TEST );
   }
   
   public int getSelection( float anX, float aY, int aScreenHeight, int aScreenWidth )
   {
      return thePoints.getSystemAt( anX, aY, aScreenHeight, aScreenWidth );
   }
}